package array;

import java.util.HashMap;
import java.util.Map;

/**
 * <h2>TwoSum</h2>
 * <p>两数之和l</p>
 * <p>CreatedAt: 2020-01-18 13:50</p>
 * <p><a href="mailto:forever2y_y@163.com">SendMailToMe</a></p>
 *
 * @author llh
 */
public class TwoSum {

    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        if (n < 2) return null;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int temp = target - nums[i];
            if (map.containsKey(temp))
                return new int[]{i, map.get(temp)};
            map.put(nums[i], i);
        }

        return null;
    }
}
